/*
 * 作者：Peter Xiang
 * 联系方式：565067150@qq.com
 * 文档: https://github.com/PxGame
 * 创建时间: 2019/10/30 11:07:10
 */

using System;
using UnityEngine;
using ActionSystem.Core;
using System.Reflection;
using UnityEditor;

namespace XMLib.AM
{
    /// <summary>
    /// ActionEditorUtility
    /// </summary>
    public static class ActionEditorUtility
    {
        public static bool HasOpenInstances(Type windowType)
        {
            UnityEngine.Object[] array = Resources.FindObjectsOfTypeAll(windowType);
            return array != null && array.Length != 0;
        }

        public static void ItemDrawer<T>(int index, ref bool selected, T obj)
        {
            if (GUILayout.Button($"{index}", selected ? AEStyles.item_head_select : AEStyles.item_head_normal, GUILayout.ExpandHeight(true), GUILayout.Width(15)))
            {
                GUI.FocusControl(null);
                selected = !selected;
            }
            if (GUILayout.Button($"{obj?.GetType().GetSimpleName()}", selected ? AEStyles.item_body_select : AEStyles.item_body_normal, GUILayout.Height(30f), GUILayout.ExpandWidth(true)))
            {
                GUI.FocusControl(null);
                selected = !selected;
            }
        }

        
        
        public static void RangeConfigDrawer(int index, ref bool selected, RangeConfig obj)
        {
            if (GUILayout.Button($"{index}", selected ? AEStyles.item_head_select : AEStyles.item_head_normal, GUILayout.ExpandHeight(true), GUILayout.Width(15)))
            {
                GUI.FocusControl(null);
                selected = !selected;
            }
            GUILayout.BeginVertical();
            {
                var fields = obj.GetType().GetFields();
                FieldInfo stateField = null;

                for (int i = 0; i < fields.Length; i++)
                {
                    if (fields[i].GetCustomAttribute<SlotTagAttribute>() != null)
                    {
                        stateField = fields[i];
                        break;
                    }
                }
                bool hasDraw = false;
                if (stateField != null)
                {
                    //获取到定义的名称
                    var names = TypeCache.GetTypesWithAttribute<SlotNameAttribute>();
                    if (names.Count > 0)
                    {
                        Type slotTypeName = names[0];
                        var values = Enum.GetValues(slotTypeName) as int[];
                        var displayNames = Enum.GetNames(slotTypeName);
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("位置ID:");
                        obj.slotID = EditorGUILayout.IntPopup(obj.slotID, displayNames, values);
                        GUILayout.EndHorizontal();
                        hasDraw = true;
                    }


                }
                if (hasDraw == false)
                {
                    obj.slotID = EditorGUILayoutEx.DrawObject("位置ID:", obj.slotID);                    
                }
            }
            {
                var fields = obj.GetType().GetFields();
                FieldInfo stateField = null;

                for (int i = 0; i < fields.Length; i++)
                {
                    if (fields[i].GetCustomAttribute<BoxTagAttribute>() != null)
                    {
                        stateField = fields[i];
                        break;
                    }
                }
                bool hasDraw = false;
                if (stateField != null)
                {
                    //获取到定义的名称
                    var names = TypeCache.GetTypesWithAttribute<BoxTypeNameAttribute>();
                    if (names.Count > 0)
                    {
                        Type boxTypeName = names[0];
                        var values = Enum.GetValues(boxTypeName) as int[];
                        var displayNames = Enum.GetNames(boxTypeName);
                        GUILayout.BeginHorizontal();
                        GUILayout.Label("碰撞盒类型:");
                        obj.boxTypeID = EditorGUILayout.IntPopup(obj.boxTypeID, displayNames, values);
                        GUILayout.EndHorizontal();
                        hasDraw = true;
                    }
                }
                if (hasDraw == false)
                {                    
                    obj.boxTypeID = EditorGUILayoutEx.DrawObject("碰撞盒类型:", obj.boxTypeID);
                }
                obj.boxTypeID = Mathf.Clamp(obj.boxTypeID,0, ActionSystemConstDefine.MaxLayer+1);
            }
            EditorGUILayoutEx.DrawObject(GUIContent.none, obj);
            GUILayout.EndVertical();
        }
    }
}